package com.unionftech.common.recaptcha

import android.app.Activity
import com.unionftech.common.CommonConstants
import com.unionftech.common.recaptcha.aliyun.AliyunCaptcha
import com.unionftech.common.recaptcha.aliyun.AliyunCaptchaConfig
import com.unionftech.common.recaptcha.aliyun.AliyunCaptchaLanguage
import com.unionftech.common.recaptcha.aliyun.AliyunCaptchaListener
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.SpUtils

class AliyunCaptchaUtil private constructor() {

    companion object {
        private const val TAG = "AliyunCaptchaUtil"
        private const val SCENE_ID = CommonConstants.Config.ALIYUN_CAPTCHA_SCENE_ID
        private const val PREFIX = CommonConstants.Config.ALIYUN_CAPTCHA_PREFIX

        // yidun 语言映射
        private val aliyunLocalMap = mutableMapOf<String, AliyunCaptchaLanguage>(
            "zh-CN" to AliyunCaptchaLanguage.cn,
            "zh-TW" to AliyunCaptchaLanguage.tw,
            "en" to AliyunCaptchaLanguage.en,
            "vn" to AliyunCaptchaLanguage.vi,
            "th" to AliyunCaptchaLanguage.th,
            "my" to AliyunCaptchaLanguage.ms,
            "ko" to AliyunCaptchaLanguage.ko,
            "ja" to AliyunCaptchaLanguage.ja,
            "fr" to AliyunCaptchaLanguage.fr,
            "pt" to AliyunCaptchaLanguage.pt,
            "es" to AliyunCaptchaLanguage.es,
            "ar" to AliyunCaptchaLanguage.ar,
            "de" to AliyunCaptchaLanguage.de,
            "it" to AliyunCaptchaLanguage.it,
            "hi" to AliyunCaptchaLanguage.ind, // 印度语
            "ru" to AliyunCaptchaLanguage.ru, // 俄语
        )

        @Volatile
        private var instance: AliyunCaptchaUtil? = null

        @JvmStatic
        fun getInstance() = instance ?: synchronized(AliyunCaptchaUtil::class.java) {
            instance ?: AliyunCaptchaUtil().also {
                instance = it
            }
        }
    }

    fun verify(
        activity: Activity,
        resultCallBack: (
            captchaVerifyParam: String?,
            jsCallBack: ((captchaResult: Boolean) -> Unit)?
        ) -> Unit
    ) {
        val appLanguageCode =
            SpUtils.getInstance().getString(CommonConstants.Key.KEY_APP_LANGUAGE_CODE)
        val builder = AliyunCaptchaConfig.Builder()
        val captchaConfig = builder.setSceneId(SCENE_ID)
            .setLanguage(aliyunLocalMap[appLanguageCode] ?: AliyunCaptchaLanguage.en)
            .setPrefix(PREFIX)
            .listener(object : AliyunCaptchaListener {
                override fun onValidate(
                    captchaVerifyParam: String,
                    jsCallBack: (captchaResult: Boolean) -> Unit
                ) {
                    resultCallBack.invoke(captchaVerifyParam, jsCallBack)
                }

                override fun onError(code: Int, msg: String?) {
                    LogUtils.e(TAG, "onError code:${code} msg : $msg")
                }

                override fun onClose() {
                    resultCallBack.invoke(RecaptchaUtil.RECAPTCHA_CANCEL, null)
                }
            })
            .build(activity)
        val aliyunCaptcha = AliyunCaptcha.getInstance()
        aliyunCaptcha.init(captchaConfig)
        aliyunCaptcha.validate()
    }

    fun destroy() {
        AliyunCaptcha.getInstance().destroy()
    }
}